package com.crealth.handler;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crealth.ConstantDefine;
import com.crealth.db.CompanyDb;
import com.crealth.db.TeamDb;
import com.crealth.db.UserDb;
import com.crealth.memcached.UserCached;
import com.crealth.model.CompanyModel;
import com.crealth.model.TeamModel;
import com.crealth.model.UserModel;
import com.crealth.msg.NetMessageData;

public class CompanyHandler {	
	
	public static NetMessageData obtainCompanyInfo(NetMessageData revData) {		
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		List<CompanyModel> companys = CompanyDb.selectAllCompany();
		int num = 0;
		if (companys != null) {
			num = companys.size();
		}		
		if (num > 0) {
			JSONArray jsonArray = new JSONArray();
			int userPos = user.getPosition();
			CompanyModel belongCompany = user.getBelongCompany();
			if (belongCompany == null && userPos != ConstantDefine.Admin_Manager ) {
				return null;
			}
			Integer belongComID = user.getBelongCompanyID();
			for (CompanyModel info : companys) {
				if (userPos >= ConstantDefine.Head_Manager || 
					userPos == ConstantDefine.Installmenter || 
					userPos == ConstantDefine.InstallDpmMGR || 
					belongCompany.topCompany() ||  
					belongComID == info.getBelongCompanyID()  ){
					jsonArray.add(info.toJSONObject());
				}
			}			
			resJson.put("companys", jsonArray.toJSONString());
		}
		
		resJson.put("comNum", num);
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	public static NetMessageData obtainAllTeam(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		if (msgJson == null) {
			return null;
		}
		
		List<TeamModel> teamls = null;
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		int pos = user.getPosition();
		CompanyModel belongCompany = user.getBelongCompany();
		if (belongCompany == null && pos != ConstantDefine.Admin_Manager) {
			return null;
		}
		if (pos > ConstantDefine.Branch_Mgr  
			|| pos == ConstantDefine.Financer 
			|| pos == ConstantDefine.Installmenter 
			|| pos == ConstantDefine.InstallDpmMGR || 
			belongCompany.topCompany() ) {
			teamls = TeamDb.selectAllTeam();
		}
		else {
			teamls = TeamDb.selectCompanyTeam(user.getBelongCompanyID());
		}
		int teamNum = teamls.size();
		resJson.put("resCode", 0);
		resJson.put("teamNum", teamNum);
		if (teamNum > 0) {
			JSONArray jsonArray = new JSONArray();
			for (TeamModel team : teamls) {
				jsonArray.add(team.toJSONObject());
			}
			resJson.put("teamInfo", jsonArray);
		}		
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	public static NetMessageData addTeam(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		Integer userID = msgJson.getInteger("id");
		int comID = msgJson.getIntValue("comID");
		if (comID <= 0) {
			return null;
		}
		String teamName = msgJson.getString("teamName");
		TeamModel team = new TeamModel();
		team.setCompanyID(comID);
		team.setTeamName(teamName);
		TeamDb.insertTeam(team);
		
		resJson.put("opUser", userID);
		resJson.put("resCode", 0);
		resJson.put("teamInfo", team.toJSONObject());
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendBranchCompanyMgr(userID, comID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, comID, sendMsg);
		ResponseMsgQueue.sendMsgToAdministration(userID, comID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, comID, sendMsg);
		
		return sendMsg;
	}
	
	public static NetMessageData addCompanyBranch(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		if (msgJson == null) {
			return null;
		}
		
		CompanyModel company = new CompanyModel();
		company.parseFromJson(msgJson);		
		CompanyDb.insertBranch(company);
		resJson.put("resCode", 0);
		resJson.put("branchInfo", company.toJSONObject());
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	public static NetMessageData saveAllTeam(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		String allTeamJson = msgJson.getString("allTeamJson");
		JSONArray jsonArray = JSONArray.parseArray(allTeamJson);
		Set<Integer> companyIDs = new HashSet<Integer>();
		if (jsonArray != null && jsonArray.size() > 0) {
			JSONArray resJsonArr = new JSONArray();
			for (int i = 0; i < jsonArray.size(); ++i) {
				JSONObject teamJson = jsonArray.getJSONObject(i);
				Integer comID = teamJson.getIntValue("comID");
				String teamName = teamJson.getString("teamName");
				TeamModel team = new TeamModel();
				team.setCompanyID(comID);
				team.setTeamName(teamName);
				if (teamJson.containsKey("teamID")) {
					int teamID = teamJson.getIntValue("teamID");
					team.setId(teamID);
					TeamDb.updateTeamInfo(team);
					UserCached.updateCompanyIDByTeamID(teamID, comID);
				} else {
					TeamDb.insertTeam(team);
				}
				resJsonArr.add(team.toJSONObject());
				companyIDs.add(comID);
			}
			resJson.put("teamInfo", resJsonArr);
		}
		
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		if (companyIDs.size() > 0) {
			for (Integer curCompanyID : companyIDs ) {
				ResponseMsgQueue.sendMsgToCustomerService(userID, curCompanyID, sendMsg);
				ResponseMsgQueue.sendMsgToFinance(userID, curCompanyID, sendMsg);
				ResponseMsgQueue.sendMsgToAdministration(userID, curCompanyID, sendMsg);				
				ResponseMsgQueue.sendMsgToMortgager(userID, curCompanyID, sendMsg);
				ResponseMsgQueue.sendMsgToMortgagerMgr(userID, curCompanyID, sendMsg);
				ResponseMsgQueue.sendBranchCompanyMgr(userID, curCompanyID, sendMsg);
			}
		}
		
		return sendMsg;
	}
	
	public static NetMessageData deleteTeam(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		int teamID = msgJson.getIntValue("teamID");
		List<UserModel> teamUserls = UserDb.selectTeamUserByTeamID(teamID);
		if (teamUserls.size() > 0) {
			resJson.put("resCode", ErrorCode.ERROR_TEAM_HASUSER);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}		
		TeamModel team = TeamDb.selectTeamByID(teamID);
		if (team == null) {
			return null;
		}
		Integer res = TeamDb.deleteTeamByTeamID(teamID);
		if (res != null && res == 1) {
			resJson.put("delTeamID", teamID);
		}		
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		
		int companyID = team.getCompanyID();
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		ResponseMsgQueue.sendBranchCompanyMgr(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToCustomerService(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToAdministration(userID, companyID, sendMsg);
		ResponseMsgQueue.sendMsgToFinance(userID, companyID, sendMsg);
		
		return sendMsg;
	}
	
	public static NetMessageData saveAllCompanyInfo(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		if (msgJson == null) {
			return null;
		}
		String allCompanyJson = msgJson.getString("allCompanyJson");
		JSONArray jsonArray = JSONArray.parseArray(allCompanyJson);
		if (jsonArray != null && jsonArray.size() > 0) {
			JSONArray resJsonArr = new JSONArray();
			for (int i = 0; i < jsonArray.size(); ++i) {
				JSONObject comJson = jsonArray.getJSONObject(i);
				CompanyModel curCompany = new CompanyModel();
				curCompany.parseFromJson(comJson);
				if (curCompany.getId() > 0) {					
					CompanyDb.updateCompanyInfo(curCompany);
				}
				resJsonArr.add(curCompany.toJSONObject());
			}
			resJson.put("companyInfos", resJsonArr);
		}
		
		resJson.put("resCode", 0);		
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	public static NetMessageData deleteCompany(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		if (msgJson == null) {
			return null;
		}
		int comID = msgJson.getIntValue("comID");
		List<CompanyModel> subComls = CompanyDb.selectSubCompanyByID(comID);
		if (subComls.size() > 0) {
			resJson.put("resCode", ErrorCode.ERROR_COM_HASSUB);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		Integer res = CompanyDb.deleteCompanyByID(comID);
		if (res != null && res == 1) {
			resJson.put("comID", comID);
		}		
		resJson.put("resCode", 0);		
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
}
